/**
 * Created with JetBrains WebStorm.
 * User: scb
 * Date: 11/4/13
 * Time: 12:36 PM
 * To change this template use File | Settings | File Templates.
 */

describe("locateController",function(){
    var locateController = null;
    var param;
    beforeEach(function(){
        //need feature
        var t_alertCtrl = App.AlertController.create();
        var t_loadingCtrl = App.LoadingController.create();
        App.LocateController.reopen({
            init:function(){
                this.set('controllers.alert', t_alertCtrl);
                this.set('controllers.loading', t_loadingCtrl);
            }
        });
        locateController = App.LocateController.create();
        param = App.i18n.getTranslations().get('locate.selectAll');
    });

    it("prepareController", function(){
        spyOn(locateController, 'getPlacesAndPlaceMarkData');
        spyOn(locateController, 'setNavItems');
        spyOn(locateController, 'selectNavItem');
        App.RouteHistory.lastRoute='';
        locateController.prepareController();
        expect(locateController.getPlacesAndPlaceMarkData).toHaveBeenCalledWith(param);
        expect(locateController.setNavItems).toHaveBeenCalled();
        expect(locateController.selectNavItem).toHaveBeenCalledWith(param);
    });

    it("setNavItems", function(){
        var emberArray = Ember.A([
            App.PublicLocateNavItem.create({
                id: 'all',
                title: 'All',
                isActive: false
            }),
            App.PublicLocateNavItem.create({
                id: 'branch',
                title: 'Branches',
                isActive: false
            }),
            App.PublicLocateNavItem.create({
                id: 'atm',
                title: 'ATM',
                isActive: false
            })
        ]);

        spyOn(locateController, 'set');
        locateController.setNavItems();
        expect(locateController.set).toHaveBeenCalledWith('navItems', emberArray);
    });

//    it("selectNavItem; if path for all", function(){
//        locateController.set("navItems",Ember.A([
//            App.PublicLocateNavItem.create({
//                id: 'all',
//                title: 'All',
//                isActive: false
//            })
//        ]));
//
//        spyOn(locateController, 'chooseFilter');
//        locateController.selectNavItem(param);
//        expect(locateController.chooseFilter).toHaveBeenCalledWith(param);
//    });
//
//    it("selectNavItem; for loop else path", function(){
//        locateController.set("navItems",Ember.A([
//            App.PublicLocateNavItem.create({
//                id: 'all',
//                title: 'All',
//                isActive: false
//            })
//        ]));
//
//        param = "elsePath";
//        locateController.selectNavItem(param);
//    });
//
//    it("selectNavItem; else path for branch", function(){
//        locateController.set("navItems",Ember.A([
//            App.PublicLocateNavItem.create({
//                id: 'branch',
//                title: 'Branches',
//                isActive: false
//            })
//        ]));
//
//        param = App.i18n.getTranslations().get('locate.selectBranch');
//        spyOn(locateController, 'chooseFilter');
//        locateController.selectNavItem(param);
//        expect(locateController.chooseFilter).toHaveBeenCalledWith(param);
//    });
//
//    it("selectNavItem; else path for atm", function(){
//        locateController.set("navItems",Ember.A([
//            App.PublicLocateNavItem.create({
//                id: 'atm',
//                title: 'ATM',
//                isActive: false
//            })
//        ]));
//
//        param = 'ATM';
//        spyOn(locateController, 'chooseFilter');
//        locateController.selectNavItem(param);
//        expect(locateController.chooseFilter).toHaveBeenCalledWith(param);
//    });

    it("getPlacesAndPlaceMarkData", function(){
        spyOn(App.api_locate, "channelDetailsCall").andCallFake(function(returnedObj){
            returnedObj();
        });
        spyOn(App.api_locate, "regionAreaPlacemark").andCallFake(function(returnedObj){
            returnedObj();
        });
        spyOn(locateController, "render");
        locateController.getPlacesAndPlaceMarkData(param);
        expect(App.api_locate.channelDetailsCall).toHaveBeenCalled();
        expect(App.api_locate.regionAreaPlacemark).toHaveBeenCalled();
    });

//    it("render", function(){
//        locateController.set("regionObj",{errorCode:"test" , test:""});
//        locateController.set("areaDictRAW",{errorCode:"test" , test:""});
//        var regionObjj = locateController.get("regionObj");
//        var areaDictRAWW = locateController.get("areaDictRAW");
//        spyOn(locateController, "hideLoading");
//        spyOn(locateController, "chooseFilter");
//
//        locateController.channelType = "all";
//        locateController.render(param);
//
//        expect(regionObjj).toEqual("test");
//        expect(areaDictRAWW).toEqual("test1");
//        expect(locateController.hideLoading).toHaveBeenCalled();
//        expect(locateController.chooseFilter).toHaveBeenCalledWith(locateController.channelType);
//    });

    it("render", function() {
        var obj = {
            regionAreaData:{
                    errorCode:undefined,test:""
            },
            areaDictData:{
                errorCode:undefined,test:""
            }
        };
        locateController.set("responseObj",obj);
        spyOn(locateController, "hideLoading");
        spyOn(locateController, "chooseFilter");
        locateController.channelType = "all";
        locateController.render(param);
        expect(locateController.hideLoading).toHaveBeenCalled();
        expect(locateController.chooseFilter).toHaveBeenCalledWith(locateController.channelType);
    });

    it("chooseFilter", function(){
//        locateController.locateModelData.set("wrappedList" , Em.A([] ));
//        locateController.wrappedListData = [];
        locateController.channelType = "atm";
        locateController.set("areaDictRAW",{atm:"test1"});
        locateController.set("areaDict", locateController.areaDictRAW[locateController.channelType]);
        spyOn(locateController, "fillInList");

        locateController.chooseFilter(locateController.channelType);
        expect(locateController.areaDict).toEqual("test1");
        expect(locateController.fillInList).toHaveBeenCalledWith(locateController.channelType);
    });

    it("fillInList", function() {
        param = "all";
        locateController.regionObj[param] = [{areas:[{},{}]},{areas:[{},{}]},{areas:[{},{}]}];
        spyOn(locateController.locateModelData.wrappedList,"set");
        locateController.locateModelData.wrappedList[0] = Em.A([{area:""},{area:""}]);
        locateController.locateModelData.wrappedList[1] = Em.A([{area:""},{area:""}]);
        locateController.locateModelData.wrappedList[2] = Em.A([{area:""},{area:""}]);
        locateController.fillInList(param);
    });

    it("selectRegion", function() {
        var region = App.Region.create({name:"te",isOpened:false});
        spyOn(region,"get").andReturn(true);
        spyOn(region,"set");
        var obj = Ember.Object.create({name:"test",isOpened:false})
        locateController.set("wrappedListData",[obj,obj,obj]);
        spyOn(locateController.wrappedListData[0],"set");
        locateController.selectRegion(region);
        expect(locateController.wrappedListData[0].set).toHaveBeenCalledWith("isOpened",false);
        expect(region.set).toHaveBeenCalledWith('isOpened', false);
    });

    it("selectArea; if branch", function(){
        var area = {};
        spyOn(locateController,"transitionTo");
        locateController.selectArea(area);
        expect(locateController.transitionTo).toHaveBeenCalled();
    });

    it("selectArea; else branch", function(){
        var area = {};
        spyOn(locateController,"transitionTo");
        locateController.selectArea(area);
        expect(locateController.transitionTo).toHaveBeenCalled();
    });

    xit("currentLocation", function(){
        spyOn(locateController, "transitionTo");
        spyOn(BreezeUtils, "getCurrentLocation").andCallThrough(func);
        var func = function(position) {
            locateController.transitionTo('publicLocate.atmBranch',position);
        };
        locateController.currentLocation(locateController);
        expect(BreezeUtils.getCurrentLocation(func)).toHaveBeenCalledWith(function(position) {
            self.transitionTo('publicLocate.atmBranch',position);
        });
    });
})